﻿using System;
using System.Collections.Generic;
using Microsoft.Office.Core;

namespace Office.Utility.Toolbar
{
    public sealed class MenuItem
    {
        public string Caption { get; set; }

        readonly List<CommandBarButton> _listButtons = new List<CommandBarButton>();

        public event EventHandler<CommandBarEventArgs> Click;

        public MenuItem(string caption)
        {
            Caption = caption;
        }

        internal void Attach(CommandBarPopup menu, WindowAdapter window, Microsoft.Office.Core.CommandBar menuBar)
        {
            var btn = (CommandBarButton)menu.Controls.Add(
                MsoControlType.msoControlButton, Type.Missing, string.Empty, Type.Missing, true);
            btn.Caption = Caption;
            btn.Style = MsoButtonStyle.msoButtonCaption;

            btn.Click += delegate
                             {
                                 var args = new CommandBarEventArgs(window, menuBar);
                                 //Use CommandBarManager's DynamicInvoke to ensure this event fires on the right thread
                                 CommandBarManager.DynamicInvoke(Click, this, args);
                             };

            _listButtons.Add(btn);

        }
	
    }
}